package cn.yudao.basic.utils;


import cn.yudao.basic.jwt.JwtUtils;
import cn.yudao.basic.jwt.Payload;
import cn.yudao.basic.jwt.RsaUtils;
import cn.yudao.basic.jwt.UserInfo;
import cn.yudao.org.domain.Employee;
import cn.yudao.user.domain.Logininfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;

import javax.servlet.http.HttpServletRequest;
import java.security.PublicKey;

public class LoginContext {

    public static Logininfo getLogininfo(HttpServletRequest request){
        // 从请求头获取token
        String token = request.getHeader("token");
        // 判断token是否存在
        if (token != null) {
            // 先获取PrivateKey(JwtUtils.class.getClassLoader().getResource(BaseConstants.JwtConstant.JWT_AUTH_PRIKEY).getFile());
            // 获取logininfo从jwt字符串中
            // 2 解析token里面内容
            PublicKey publicKey = null;
            try {
                publicKey = RsaUtils.getPublicKey(JwtRasHolder.INSTANCE.getJwtRsaPubData());
            } catch (Exception e) {
                e.printStackTrace();
            }
            Payload<UserInfo> payload = JwtUtils.getInfoFromToken(token, publicKey, UserInfo.class);
            Logininfo logininfo = payload.getUserInfo().getLogininfo();
            return logininfo;
        }
        return null;
    }


}
